package com.zzx.boot.servlet.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.zzx.boot.constant.PreFix;
import com.zzx.boot.constant.Time;
import com.zzx.boot.entity.dto.UserInfoDto;
import com.zzx.boot.entity.table.User;
import com.zzx.boot.entity.table.UserInfo;
import com.zzx.boot.entity.vo.UserInfoVo;
import com.zzx.boot.mapper.UserInfoMapper;
import com.zzx.boot.servlet.UserInfoService;
import com.zzx.boot.utils.RedisUtils;
import com.zzx.boot.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author tan
 * @version 1.0
 * @date 2023/7/8 10:19
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {



    @Override
    public String updateInfo(UserInfoDto userInfoDto) {

        UserInfo info = getUserInfoByUserId();
        BeanUtils.copyProperties(userInfoDto,info);
        int i = baseMapper.updateById(info);

        return i==1?"更新成功":"更新失败";
    }

    private UserInfo getUserInfoByUserId() {
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getUserId, SecurityUtils.getUserId());
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public UserInfoVo getUserInfo() {
        // 通过用户id获取
        UserInfoVo vo = new UserInfoVo();
        User user = SecurityUtils.getLoginUser().getUser();
        BeanUtils.copyProperties(user,vo);
        // 获取资料
        UserInfo info = getUserInfoByUserId();
        BeanUtils.copyProperties(info,vo);
        vo.setId(info.getId().toString());
        return vo;
    }
}
